package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.GoodsType;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/19 17:24
 * description:
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {
    QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public List<GoodsType> findTypeListBylevel(int level) {
        String sql=" select id,name,level,parent from tb_goods_type where level=? ";
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class),level);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GoodsType findById(int typeid) {
        String sql=" select id,name,level,parent from tb_goods_type where id=? ";
        try {
            GoodsType goodsType = queryRunner.query(sql, new BeanHandler<>(GoodsType.class),typeid);
            return goodsType;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> findAllGoodsType() {
        String sql=" select id,name,level,parent from tb_goods_type ";
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class));
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(GoodsType goodsType) {
        String sql=" insert into tb_goods_type (id,name,level,Parent) values (null,?,?,?) ";
        try {
            queryRunner.update(sql,goodsType.getName(),goodsType.getLevel(),goodsType.getParent());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<GoodsType> findGoodsTypeByLevel(String level) {
        String sql=" select id,name,level,parent from tb_goods_type where level=?";
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class),level);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> findGoodsTypeByName(String name) {
        String sql=" select id,name,level,parent from tb_goods_type where name like '%"+name+"%' ";
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class));
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<GoodsType> findGoodsTypeByNameAndLevel(String level, String name) {
        String sql=" select id,name,level,parent from tb_goods_type where level=? and name like '%"+name+"%'  ";
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class),level);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
